package com.fy56.platform.logistics.controller;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.fy56.platform.logistics.common.BaseController;
import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.model.Company;
import com.fy56.platform.logistics.model.DeliverGoodsWeb;
import com.fy56.platform.logistics.service.wx.IDeliverGoodsWebSetService;
import com.fy56.platform.logistics.service.wx.ICompanyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.fy56.platform.logistics.model.User;

import javax.servlet.http.HttpServletRequest;

/**
 * 发货网点设置controller
 * Created by wxm on 2019-5-15
 */
@SuppressWarnings("ALL")
@RestController
@RequestMapping("/controller/deliverGoodsWebSet")
public class DeliverGoodsWebSetController extends BaseController {

    @Autowired
    private IDeliverGoodsWebSetService iDeliverGoodsWebSetService;

    @Autowired
    private ICompanyInfoService iCompanyInfoService;
    /**
     * 查询发货网点
     *
     * @param keyId
     * @return
     */
    @RequestMapping(value = "/findDeliverGoodsWeb", method = RequestMethod.POST)
    @ResponseBody
    public ResultModel findDeliverGoodsWeb(DeliverGoodsWeb web, HttpServletRequest request){
        return iDeliverGoodsWebSetService.select(web);
    }

    /**
     * 获取网点和子网点
     *
     * @param companyCode
     * @return
     */
    @RequestMapping(value = "/getWeb", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> getWeb(String companyCode, HttpServletRequest request){
        if(!StringUtils.isBlank(companyCode)){
            Company model = iCompanyInfoService.getCompanyByCode(companyCode);
            return new ResponseEntity<ResultModel>(iDeliverGoodsWebSetService.getWeb(model, companyCode), HttpStatus.OK);
        }
        return new ResponseEntity<ResultModel>(iDeliverGoodsWebSetService.getWeb(null, companyCode), HttpStatus.OK);
    }

    /**
     * 保存
     *
     * @param server
     * @param request
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> save(String companyCode, String childCodes, HttpServletRequest request) {

        try{
            User user = (User)request.getSession().getAttribute("sessionUser");
            if (StringUtils.isBlank(companyCode)) {
                return new ResponseEntity<ResultModel>(ResultModel.error("发货网点为空，请选择要设置的发货网点"), HttpStatus.OK);
            } else if (StringUtils.isBlank(childCodes)) {
                return new ResponseEntity<ResultModel>(ResultModel.error("发货网点为空，请选择要设置的发货网点"), HttpStatus.OK);
            } else {
                Company model = iCompanyInfoService.getCompanyByCode(companyCode);
                return new ResponseEntity<ResultModel>(iDeliverGoodsWebSetService.insert(model, companyCode, childCodes, user.getLoginName()), HttpStatus.OK);
            }
        }catch (Exception e){
            e.printStackTrace();
            //保存系统业务日志
            //LogUtils.saveLog(request,"失败",news.getId().toString(),null,"newsManger","update","编辑", JSON.toJSONString(news));
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }
    }

    /**
     * 批量删除
     *
     * @param keyIds
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<ResultModel> delete(String keyIds, HttpServletRequest request) {
        try{
            User user = (User)request.getSession().getAttribute("sessionUser");
            if (StringUtils.isBlank(keyIds)) {
                return new ResponseEntity<ResultModel>(ResultModel.error("发货网点为空，请选择要删除的发货网点"), HttpStatus.OK);
            } else {
                return new ResponseEntity<ResultModel>(iDeliverGoodsWebSetService.deleteByCompanyCodes(keyIds, user.getLoginName()), HttpStatus.OK);
            }

        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<ResultModel>(new ResultModel(EXCEPTION_MSG), HttpStatus.OK);
        }
    }



}
